package com.javalong.androidmorning.wechat.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.javalong.androidmorning.dao.KeywordDao;
import com.javalong.androidmorning.pojo.Keyword;

@Service
public class KeywordService {

	@Autowired
	private KeywordDao keywordDao;

	/**
	 * 找到最合适的关键字
	 * 
	 * @return
	 */
	public Keyword matchKeyword(String content) {
		Keyword keyword = keywordDao.findByKeyword(content);
		if (keyword != null) {
			return keyword;
		}
		Keyword matchKeyword = keywordDao.matchKeyword(content);
		return matchKeyword;
	}

	public List<Keyword> getKeywordList(int page, int pageSize) {
		return keywordDao.findAll(page * pageSize, pageSize);
	}

	public void deleteKeyword(int id) {
		keywordDao.delete(id);
	}

	public void saveKeyword(Keyword keyword) {
		Date now = new Date();
		keyword.setCreateAt(now);
		keyword.setUpdateAt(now);
		keywordDao.save(keyword);
	}
	
	public int getCount(){
		return keywordDao.getCount();
	}
	
	public void updateKeyword(Keyword keyword) {
		Date now = new Date();
		keyword.setUpdateAt(now);
		keywordDao.update(keyword);
	}
	
	public Keyword findKeywordById(int id){
		return keywordDao.findById(id);
	}
}
